//app.js
const wx = require('./libs/wx.js');
const { Promise } = wx;
const {getCompanyDetail} = require('./apis');
const config = require('./config');

App({
    onLaunch() {
    },

    getUserInfo() {
        if (this.globalData.userInfo) {
            return new Promise(resovle => resovle(this.globalData.userInfo));
        } else {
            //调用登录接口
            return wx.getUserInfo({
                withCredentials: false,
            }).then((res) => {
                this.globalData.userInfo = res.userInfo;
                return this.globalData.userInfo;
            })
        }
    },

    getCompanyDetail(){
        if (this.globalData.detail) {
            return new Promise(resovle => resovle(this.globalData.detail));
        } else {
            //调用登录接口
            return getCompanyDetail().then((res) => {
                this.globalData.detail = res.data.data;

                if (this.globalData.detail.company && !this.globalData.detail.company.phoneNum)
                    this.globalData.detail.company.phoneNum = config.phoneNum;
                this.globalData.detail.company.phoneNum = this.globalData.detail.company.phoneNum + '';


                return this.globalData.detail;
            })
        }
    },

    globalData: {
        userInfo: null,

        detail: null
    }
});
